//package com.lyq.pdf.pdfbox;
//
//import org.apache.pdfbox.cos.COSName;
//import org.apache.pdfbox.pdmodel.PDDocument;
//import org.apache.pdfbox.pdmodel.PDPage;
//import org.apache.pdfbox.pdmodel.PDResources;
//import org.apache.pdfbox.pdmodel.common.PDRectangle;
//import org.apache.pdfbox.pdmodel.font.PDFont;
//import org.apache.pdfbox.text.PDFTextStripper;
//import org.apache.pdfbox.text.PDFTextStripperByArea;
//import org.apache.pdfbox.text.TextPosition;
//
//import java.awt.geom.Rectangle2D;
//import java.io.File;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// *
// */
//public class FindTextLocationInPDF {
//    public static void main(String[] args) throws IOException {
//        // Load PDF document
//        PDDocument document = PDDocument.load(new File("example.pdf"));
//
//        // Create a list to store the positions of the desired text
//        List<Rectangle2D> positions = new ArrayList<>();
//
//        // Loop through each page in the document
//        for (PDPage page : document.getPages()) {
//            // Get the resources for the page
//            PDResources resources = page.getResources();
//
//            // Create a PDFTextStripper object to extract text from the page
//            PDFTextStripperByArea stripper = new PDFTextStripperByArea();
//
//            // Set the region to search for text
//            float x = 100; // x coordinate of the upper-left corner
//            float y = 100; // y coordinate of the upper-left corner
//            float width = 200; // width of the area
//            float height = 100; // height of the area
//            Rectangle2D region = new Rectangle2D.Float(x, y, width, height);
//            stripper.addRegion("region", region);
//
//            // Set the font size and style for the text
////            PDFont font = resources.getFont(COSName.HELVETICA_BOLD);
////            stripper.setFont(font);
////            stripper.setFontSize(12);
//
//            // Extract the text from the region
//            stripper.extractRegions(page);
//
//            // Get the list of TextPosition objects for the specified region
//            List<TextPosition> textPositions = stripper.getTextForRegion("region");
//
//            // Loop through the list of TextPosition objects to find the desired text
//            String searchText = "Hello World";
//            for (TextPosition text : textPositions) {
//                String str = text.getUnicode();
//
//                // Check if the current text position matches the desired text
//                if (str.contains(searchText)) {
//                    // Get the bounding box for the current text position
//                    float x0 = text.getXDirAdj();
//                    float y0 = text.getYDirAdj();
//                    float width0 = text.getWidthDirAdj();
//                    float height0 = text.getHeightDir();
//
//                    // Transform the bounding box to the page coordinates
//                    PDRectangle cropBox = page.getCropBox();
//                    float x1 = cropBox.getLowerLeftX() + x0;
//                    float y1 = cropBox.getUpperRightY() - y0;
//                    float x2 = x1 + width0;
//                    float y2 = y1 - height0;
//                    Rectangle2D position = new Rectangle2D.Float(x1, y1, x2 - x1, y2 - y1);
//
//                    // Add the position to the list
//                    positions.add(position);
//                }
//            }
//        }
//
//        // Print the positions of the desired text
//        for (Rectangle2D position : positions) {
//            System.out.println(position);
//        }
//
//        // Close the document
//        document.close();
//    }
//
//    class abc {
//        public void main(String[] args) throws IOException {
//            // Load PDF document
//            PDDocument document = PDDocument.load(new File("example.pdf"));
//
//            // Create a list to store the positions of the desired text
//            List<TextPosition> positions = new ArrayList<>();
//
//            // Create a PDFTextStripper object to extract text from the document
//            PDFTextStripper stripper = new PDFTextStripper() {
//                @Override
//                protected void writeString(String str, List<TextPosition> textPositions) throws IOException {
//                    // Loop through each character in the string
//                    for (int i = 0; i < str.length(); i++) {
//                        // Get the current character
//                        char c = str.charAt(i);
//
//                        // Check if the current character matches the desired text
//                        String searchText = "Hello World";
//                        if (c == searchText.charAt(0)) {
//                            // Get the TextPosition object for the current character
//                            TextPosition text = textPositions.get(i);
//
//                            // Add the TextPosition object to the list
//                            positions.add(text);
//                        }
//                    }
//                }
//            };
//
//            // Loop through each page in the document
//            for (PDPage page : document.getPages()) {
//                // Extract the text from the page
//                stripper.extractRegions(page);
//
//                // Clear the list of TextPosition objects for the next page
//                positions.clear();
//            }
//
//            // Print the positions of the desired text
//            for (TextPosition position : positions) {
//                System.out.println("Page " + position.getPageNumber() + ", x=" + position.getXDirAdj() + ", y=" + position.getYDirAdj());
//            }
//
//            // Close the document
//            document.close();
//        }
//    }
//}
